package com.institute.entity.dto;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.common.util.TreeNode;
import com.institute.entity.Institute;

public class InstituteDTO {
	
	private String code;
	private String name;
	private Integer capacity;
	private Integer stuffNum;
	private String principal;
	private Long fatherId;
	private Integer level;
	private List<InstituteDTO> children = new ArrayList<InstituteDTO>();
	private Long deptId;
	
	public InstituteDTO() {
		super();
	}
	


	public InstituteDTO(Institute dept,Integer stuffNum,String principal,Long fatherId) {
		super();
		this.deptId = dept.getId();
		this.code = dept.getCode();
		this.name = dept.getName();
		this.capacity = dept.getCapacity();
		this.stuffNum = stuffNum;
		this.principal = principal;
		this.fatherId = fatherId;
		this.level = dept.getLevel();
	}



	public InstituteDTO(Long deptId, String code, String name, Integer capacity, Integer stuffNum, String principal,
			Long fatherId, Integer level, List<InstituteDTO> children) {
		super();
		this.deptId = deptId;
		this.code = code;
		this.name = name;
		this.capacity = capacity;
		this.stuffNum = stuffNum;
		this.principal = principal;
		this.fatherId = fatherId;
		this.level = level;
		this.children = children;
	}

	


	public Long getDeptId() {
		return deptId;
	}



	public void setDeptId(Long deptId) {
		this.deptId = deptId;
	}



	public String getCode() {
		return code;
	}



	public void setCode(String code) {
		this.code = code;
	}



	public String getName() {
		return name;
	}



	public void setName(String name) {
		this.name = name;
	}



	public Integer getCapacity() {
		return capacity;
	}



	public void setCapacity(Integer capacity) {
		this.capacity = capacity;
	}



	public Integer getStuffNum() {
		return stuffNum;
	}



	public void setStuffNum(Integer stuffNum) {
		this.stuffNum = stuffNum;
	}



	public String getPrincipal() {
		return principal;
	}



	public void setPrincipal(String principal) {
		this.principal = principal;
	}



	public Long getFatherId() {
		return fatherId;
	}



	public void setFatherId(Long fatherId) {
		this.fatherId = fatherId;
	}



	public Integer getLevel() {
		return level;
	}



	public void setLevel(Integer level) {
		this.level = level;
	}



	public List<InstituteDTO> getChildren() {
		return children;
	}



	public void setChildren(List<InstituteDTO> children) {
		this.children = children;
	}
	
	@Override
	public String toString() {
		return "InstituteDTO [code=" + code + ", name=" + name + ", capacity=" + capacity + ", stuffNum=" + stuffNum
				+ ", principal=" + principal + ", fatherId=" + fatherId + ", level=" + level + ", children=" + children
				+ ", deptId=" + deptId + "]";
	}



	public static Specification<Institute> getSpecification( final InstituteDTO instituteDTO)
	{
		Specification<Institute> spec = new Specification<Institute>() {
			public Predicate toPredicate(Root<Institute> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				
				 if(null != instituteDTO && !StringUtils.isEmpty(instituteDTO.getName())) {
					 Predicate  p1 =  cb.like(root.get("name").as(String.class),"%"+instituteDTO.getName()+"%");
					 list.add(p1);
				 }
				 if(null != instituteDTO && !StringUtils.isEmpty(instituteDTO.getCode())) {
					 Predicate  p3 =  cb.like(root.get("code").as(String.class),"%"+instituteDTO.getCode()+"%");
					 list.add(p3);
				 }
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;
	}
	public static TreeNode<InstituteDTO> getTreeNode(List<InstituteDTO> depts){
		TreeNode treeNode = new TreeNode<InstituteDTO>();
		Integer total_level = 0;
		for(InstituteDTO dto:depts) {
			if(dto.getLevel() > total_level)total_level = dto.getLevel();
		}
		for(Integer _level=total_level;_level>0;_level--) {
			if(_level == total_level) {
				for(InstituteDTO dto:depts) {
					if(dto.getLevel() == total_level) {
						treeNode.getChildren().add(dto);
					}
				}
			}else {
				List<Long> removeIds = new ArrayList();
				for(InstituteDTO dto:depts) {
					if(dto.getLevel()==_level) {
						int childIdx = 0;
						int childNum = treeNode.getChildren().size();
						for(int index = 0;index < childNum;index++) {
							InstituteDTO child = (InstituteDTO)treeNode.getChildren().get(index);
							if(child.getFatherId()==dto.getDeptId()) {
								dto.getChildren().add(child);
								removeIds.add(child.getDeptId());
							}else {
							}
							childIdx++;
						}	
						treeNode.getChildren().add(dto);
					}
					
				}
				for(Long id:removeIds) {
					for(int i=0;i<treeNode.getChildren().size();i++) {
						InstituteDTO remove = (InstituteDTO) treeNode.getChildren().get(i);
						if(remove.getDeptId()==id)treeNode.getChildren().remove(i);
					}
				}
			}
		}
		return treeNode;
		
	}
}
